package com.csu.tour.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csu.tour.entity.Room;
import com.csu.tour.entity.StarHotel;
import com.csu.tour.mapper.RoomMapper;
import com.csu.tour.mapper.StarHotelMapper;
import com.csu.tour.service.StarHotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

// StarHotelServiceImpl.java
@Service
public class StarHotelServiceImpl implements StarHotelService {

    @Autowired
    private StarHotelMapper starHotelMapper;

    @Autowired
    private RoomMapper roomMapper;

    @Override
    public List<StarHotel> getStarHotelWithRooms(int hotelId, int pageNum, int pageSize) {
        // 使用MyBatis Plus的lambda表达式来实现分页查询
        QueryWrapper<StarHotel> starHotelQuery = new QueryWrapper<>();
        starHotelQuery.eq("hotel_id", hotelId);

        List<StarHotel> starHotels = starHotelMapper.selectPage(
                new Page<>(pageNum, pageSize), starHotelQuery).getRecords();

        // 查询对应的房间信息
        for (StarHotel starHotel : starHotels) {
            QueryWrapper<Room> roomQuery = new QueryWrapper<>();
            roomQuery.eq("hotel_id", starHotel.getHotelId());
            roomQuery.last("ORDER BY room_id"); // 根据房间ID排序，可根据需求调整

            // 分页查询房间数据
            Page<Room> roomPage = new Page<>(pageNum, pageSize);
            List<Room> rooms = roomMapper.selectPage(roomPage, roomQuery).getRecords();

            // 将房间信息添加到酒店信息中
            starHotel.setRooms(rooms);
        }

        return starHotels;
    }
}
